package com.koo.common.utils;

import com.koo.common.exception.RRException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.*;

public class HttpClientUtil {
    private static Logger log = LoggerFactory.getLogger(HttpClientUtil.class);
    private static RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(45000).setConnectTimeout(45000).setConnectionRequestTimeout(45000).build();

    public HttpClientUtil() {
    }

    public static String formatCurrDateTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date());
    }

    public static String postJson(String url, String jsonParams) {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse httpresponse = null;
        String content = null;

        try {
            httpClient = HttpClients.createDefault();
            HttpPost post = new HttpPost(url);
            post.setConfig(requestConfig);
            StringEntity postingString = new StringEntity(jsonParams, "UTF-8");
            post.setEntity(postingString);
            post.addHeader("Content-Type", "application/json;charset=UTF-8");
            httpresponse = httpClient.execute(post);
            content = EntityUtils.toString(httpresponse.getEntity());
        } catch (Exception var14) {
            throw new RuntimeException("网络请求失败" + var14.getMessage());
        } finally {
            try {
                if (httpresponse != null) {
                    httpresponse.close();
                }
                if (httpClient != null) {
                    httpClient.close();
                }
            } catch (IOException var13) {
            }
        }

        return content;
    }

    public static String post(String url, String[]... params) {
        String jsonStr = null;
        CloseableHttpClient httpclient = null;

        try {
            httpclient = HttpClients.createDefault();
            HttpPost httpost = new HttpPost(url);
            List<NameValuePair> postParams = new ArrayList();
            String[][] arrs = params;
            int len = params.length;

            for(int i = 0; i < len; ++i) {
                String[] objs = arrs[i];
                postParams.add(new BasicNameValuePair(objs[0], objs[1]));
            }
            httpost.setEntity(new UrlEncodedFormEntity(postParams, "utf-8"));
            HttpResponse response = httpclient.execute(httpost);
            jsonStr = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (httpclient != null) {
                    httpclient.close();
                }
            } catch (IOException var17) {
            }
        }
        return jsonStr;
    }

    public static String post(String url, Map<String, String> params) {
        String body = "";
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;
        String res;
        try {
            client = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setConfig(requestConfig);
            List<NameValuePair> nvps = new ArrayList();
            if (params != null) {
                Iterator it = params.entrySet().iterator();

                while(it.hasNext()) {
                    Map.Entry<String, String> entry = (Map.Entry)it.next();
                    nvps.add(new BasicNameValuePair((String)entry.getKey(), (String)entry.getValue()));
                }
            }

            String encode = "UTF-8";
            httpPost.setEntity(new UrlEncodedFormEntity(nvps, encode));
            httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
            httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            response = client.execute(httpPost);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                body = EntityUtils.toString(entity, encode);
            }
            EntityUtils.consume(entity);
            res = body;
        } catch (Exception var18) {
            throw new RRException("httpclient httpPost 失败", var18);
        } finally {
            try {
                if (response != null) {
                    response.close();
                }

                if (client != null) {
                    client.close();
                }
            } catch (IOException var17) {
            }

        }

        return res;
    }

    public static String get(String url) {
        String body = null;
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse httpresponse = null;

        try {
            httpClient = HttpClients.createDefault();
            URL url2 = new URL(url);
            URI uri = new URI(url2.getProtocol(), url2.getHost() + ":" + url2.getPort(), url2.getPath(), url2.getQuery(), (String)null);
            HttpGet httpget = new HttpGet(uri);
            httpget.setConfig(requestConfig);
            httpresponse = httpClient.execute(httpget);
            HttpEntity entity = httpresponse.getEntity();
            body = EntityUtils.toString(entity);
            log.info("传回：" + body);
        } catch (Exception var15) {
            throw new RRException("httpclient get 失败", var15);
        } finally {
            try {
                if (httpresponse != null) {
                    httpresponse.close();
                }

                if (httpClient != null) {
                    httpClient.close();
                }
            } catch (IOException var14) {
            }

        }

        return body;
    }
}
